﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Library.Helper;
using ZFS.Model;
using ZFSData;

namespace ZFS.ServerLibrary
{
    public partial class BaseService : IBaseService
    {
        /// <summary>
        /// 获取用户组集合
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public byte[] GetGroups(string search)
        {
            try
            {
                var GroupList = Factory.GetGroupManager().GetGroups(search);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = GroupList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取用户组集合", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 获取指定组用户
        /// </summary>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public byte[] GetGroupUsers(string groupID)
        {
            try
            {
                var userViewList = Factory.GetGroupManager().GetGroupUsers(groupID);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = userViewList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取指定组用户", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 获取指定组所含权限
        /// </summary>
        /// <param name="groupCode"></param>
        /// <returns></returns>
        public byte[] GetGroupFuncs(string groupCode)
        {
            try
            {
                var gpFuncList = Factory.GetGroupManager().GetGroupFuncs(groupCode);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = gpFuncList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取指定组所含权限", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 更新用户组权限
        /// </summary>
        /// <param name="group"></param>
        /// <param name="userList"></param>
        /// <param name="funcList"></param>
        /// <returns></returns>
        public byte[] UpdateGroupFunc(byte[] group, byte[] userList, byte[] funcList)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(group) as tb_Group;
                var Userlist = ZipTools.DecompressionObject(userList) as List<View_GroupUser>;
                var FuncList = ZipTools.DecompressionObject(funcList) as List<tb_GroupFunc>;
                bool result = Factory.GetGroupManager().UpdateGroupFunc(Group, Userlist, FuncList);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("更新用户组权限", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 删除组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public byte[] RemovebyGroup(int id)
        {
            try
            {
                bool result = Factory.GetGroupManager().Remove(id);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("删除组", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 条件查询数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public byte[] GetModelsByGroup(byte[] search)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(search) as tb_Group;
                var gpFuncList = Factory.GetGroupManager().GetModels(Group);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = gpFuncList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("条件查询数据", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 获取组-分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <param name="Asc"></param>
        /// <returns></returns>
        public byte[] GetPagingModelsByGroup(int pageIndex, int pageSize,
            byte[] search,
            bool Asc = false)
        {
            try
            {
                int totalCount;
                var Group = ZipTools.DecompressionObject(search) as tb_Group;
                var gpFuncList = Factory.GetGroupManager().GetPagingModels(pageIndex, pageSize, out totalCount, Group, Asc);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = gpFuncList, TotalCount = totalCount });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取组-分页", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 删除组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] DeleteEntityByGroup(byte[] entity)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(entity) as tb_Group;
                var result = Factory.GetGroupManager().DeleteEntity(Group);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("删除组", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 更新组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] UpdateEntityByGroup(byte[] entity)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(entity) as tb_Group;
                var result = Factory.GetGroupManager().UpdateEntity(Group);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("更新组", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 新增组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] AddEntityByGroup(byte[] entity)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(entity) as tb_Group;
                var result = Factory.GetGroupManager().AddEntity(Group);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("新增组", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 检查组是否存在
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public byte[] ExistEntityByGroup(byte[] search)
        {
            try
            {
                var Group = ZipTools.DecompressionObject(search) as tb_Group;
                var result = Factory.GetGroupManager().ExistEntity(Group);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });

            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("检查组是否存在", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }
    }
}
